package NewCoder;

public class BM85 {
    public String solve (String IP) {
        // write code here
        if(isIpv4(IP)){
            return "IPv4";
        }else if(isIpv6(IP)){
            return "IPv6";
        }else{
            return "Neither";
        }
    }
    private boolean isIpv4(String ip){
        if(ip.indexOf('.')==-1){
            return false;
        }
        String[] s=ip.split("\\.",-1);
        if(s.length!=4) return false;
        for(int i=0;i<s.length;i++){
            //有一个分割为0 说明两个点相连
            if(s[i].length()==0) return false;
            if(s[i].length()<0 || s[i].length()>3 || (s[i].charAt(0)=='0'&&s[i].length()!=1)) return false;
            int num=0;
            for(int j=0;j<s[i].length();j++){
                char c=s[i].charAt(j);
                if(c<'0' || c>'9') return false;
                num=num*10+(int)(c-'0');
                if(num<0 || num>255) return false;
            }
        }
        return true;
    }
    private boolean isIpv6(String ip){
        if(ip.indexOf(':')==-1) return false;
        String[] s=ip.split(":",-1);
        if(s.length!=8) return false;
        for(int i=0;i<s.length;i++){
            if(s[i].length()==0 || s[i].length()>4) return false;
            for(int j=0;j<s[i].length();j++){
                char c=s[i].charAt(j);
                boolean expr=((c>='0' && c<='9') || (c>='a'&& c<='f') || (c>='A' && c<='F'));
                if(!expr) return false;
            }
        }
        return true;
    }
}
